package lei.j_java泛型_匿名内部类_反射.反射;

/**
 * @className: InheritanceReflectionExample
 * @author: xl
 * @date: 2025/7/26 19:04
 * @Version: 1.0
 * @description:
 */

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;

public class InheritanceReflectionExample {
    public static void main(String[] args) throws Exception {
        Class<?> clazz = Class.forName("lei.j_java泛型_匿名内部类_反射.反射.TeacherDto");

        System.out.println("=== 获取当前类和父类的所有字段 ===");
        printAllFields(clazz);

        System.out.println("\n=== 获取当前类和父类的所有方法 ===");
        printAllMethods(clazz);


        // 获取当前类声明的所有字段（不包括父类）
        Field[] declaredFields = clazz.getDeclaredFields();

        // 获取当前类及其父类的所有公共字段
        Field[] fields = clazz.getFields();

        for (Field field : fields) {
            System.out.println(field.getName()+":"+field.getType().getSimpleName());
        }



        // 获取当前类声明的所有方法（不包括父类）
        Method[] declaredMethods = clazz.getDeclaredMethods();

        // 获取当前类及其父类的所有公共方法
        Method[] methods = clazz.getMethods();

    }

    // 递归获取所有父类的字段
    public static void printAllFields(Class<?> clazz) {
        System.out.println("类 " + clazz.getSimpleName() + " 的字段：");

        // 获取当前类的所有字段（包括私有字段）
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field : declaredFields) {
            System.out.println("  " + field.getName() + " : " + field.getType().getSimpleName());
        }

        // 获取父类字段
        Class<?> superClass = clazz.getSuperclass();
        if (superClass != null && superClass != Object.class) {
            System.out.println("父类 " + superClass.getSimpleName() + " 的字段：");
            Field[] superFields = superClass.getDeclaredFields();
            for (Field field : superFields) {
                System.out.println("  " + field.getName() + " : " + field.getType().getSimpleName());
            }
        }
    }


    // 递归获取所有父类的方法
    public static void printAllMethods(Class<?> clazz) {
        System.out.println("类 " + clazz.getSimpleName() + " 的方法：");

        // 获取当前类的所有方法（包括私有方法）
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method method : declaredMethods) {
            System.out.println("  " + method.getName() + " : " + method.getReturnType().getSimpleName());
        }

        // 获取父类方法
        Class<?> superClass = clazz.getSuperclass();
        if (superClass != null && superClass != Object.class) {
            System.out.println("父类 " + superClass.getSimpleName() + " 的方法：");
            Method[] superMethods = superClass.getDeclaredMethods();
            for (Method method : superMethods) {
                System.out.println("  " + method.getName() + " : " + method.getReturnType().getSimpleName());
            }
        }
    }

    // 获取类及其所有父类的字段
    public static void getAllFields(Class<?> clazz) {
        Class<?> currentClass = clazz;
        while (currentClass != null && currentClass != Object.class) {
            System.out.println("类 " + currentClass.getSimpleName() + " 的字段：");
            Field[] fields = currentClass.getDeclaredFields();
            for (Field field : fields) {
                System.out.println("  " + Modifier.toString(field.getModifiers()) +
                        " " + field.getType().getSimpleName() +
                        " " + field.getName());
            }
            currentClass = currentClass.getSuperclass();
            System.out.println();
        }
    }

    // 获取类及其所有父类的方法
    public static void getAllMethods(Class<?> clazz) {
        Class<?> currentClass = clazz;
        while (currentClass != null && currentClass != Object.class) {
            System.out.println("类 " + currentClass.getSimpleName() + " 的方法：");
            Method[] methods = currentClass.getDeclaredMethods();
            for (Method method : methods) {
                System.out.println("  " + Modifier.toString(method.getModifiers()) +
                        " " + method.getReturnType().getSimpleName() +
                        " " + method.getName() +
                        " 参数个数: " + method.getParameterCount());
            }
            currentClass = currentClass.getSuperclass();
            System.out.println();
        }
    }

}

// 示例父类
class Person2 {
    protected String id;
    private String address;
    public String sex ;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    protected void displayInfo() {
        System.out.println("Person Info");
    }

    private void privateMethod() {
        System.out.println("Private method in Person");
    }

    @Override
    public String toString() {
        return "Person2{" +
                "id='" + id + '\'' +
                ", address='" + address + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

// 示例子类
class TeacherDto extends Person2 {
    private String name;
    private int age;
    private String Job;

    public TeacherDto() {
    }

    public TeacherDto(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getJob() {
        return Job;
    }

    public void setJob(String job) {
        Job = job;
    }

    public int add(int a, int b) {
        return a + b;
    }

    private int multiply(int a, int b) {
        return a * b;
    }

    @Override
    public String toString() {
        return "TeacherDto{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", Job='" + Job + '\'' +
                '}';
    }
}
